package io.kiki.stack.netty.command;

import io.kiki.stack.netty.exception.RpcServerException;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;

@Slf4j
@Setter
@Getter
public class CommandFactoryImpl implements CommandFactory {
    @Override
    public AbstractRequestCommand createRequest(Object requestObject) {
        return new AbstractRequestCommand(requestObject);
    }

    @Override
    public ResponseCommand createTimeoutResponse(InetSocketAddress address) {
        ResponseCommand responseCommand = new ResponseCommand();
        responseCommand.setResponseStatus(ResponseType.timeout);
        responseCommand.setResponseTimeMillis(System.currentTimeMillis());
        responseCommand.setResponseHost(address);
        return responseCommand;
    }

    @Override
    public Command createChannelClosedResponse(InetSocketAddress address, String message) {
        ResponseCommand responseCommand = new ResponseCommand();
        responseCommand.setResponseStatus(ResponseType.channel_closed);
        responseCommand.setResponseTimeMillis(System.currentTimeMillis());
        responseCommand.setResponseHost(address);
        return responseCommand;
    }

    @Override
    public Command createSendFailedResponse(final InetSocketAddress address, Throwable throwable) {
        ResponseCommand responseCommand = new ResponseCommand();
        responseCommand.setResponseStatus(ResponseType.client_send_error);
        responseCommand.setResponseTimeMillis(System.currentTimeMillis());
        responseCommand.setResponseHost(address);
        responseCommand.setCause(throwable);
        return responseCommand;
    }

    @Override
    public AbstractResponseCommand createResponse(final Object responseObject, final Command requestCmd) {
        AbstractResponseCommand response = new AbstractResponseCommand(requestCmd.getId(), responseObject);
        if (null != responseObject) {
            response.setResponseClass(responseObject.getClass().getName());
        } else {
            response.setResponseClass(null);
        }

        response.setProtocolSwitch(requestCmd.getProtocolSwitch());
        response.setResponseStatus(ResponseType.success);
        return response;
    }

    @Override
    public AbstractResponseCommand createExceptionResponse(int id, String errMsg) {
        return createExceptionResponse(id, errMsg, null);
    }

    @Override
    public AbstractResponseCommand createExceptionResponse(int id, String errMsg, final Throwable t) {
        AbstractResponseCommand response = null;
        if (null == t) {
            response = new AbstractResponseCommand(id, createServerException(errMsg));
        } else {
            response = new AbstractResponseCommand(id, createServerException(errMsg, t));
        }
        response.setResponseClass(RpcServerException.class.getName());
        response.setResponseStatus(ResponseType.server_exception);
        return response;
    }

    @Override
    public AbstractResponseCommand createExceptionResponse(int id, ResponseType responseType) {
        AbstractResponseCommand responseCommand = new AbstractResponseCommand();
        responseCommand.setId(id);
        responseCommand.setResponseStatus(responseType);
        return responseCommand;
    }

    @Override
    public AbstractResponseCommand createExceptionResponse(int id, ResponseType responseType, Throwable t) {
        AbstractResponseCommand responseCommand = this.createExceptionResponse(id, responseType);
        responseCommand.setResponseObject(createServerException(null, t));
        responseCommand.setResponseClass(RpcServerException.class.getName());
        return responseCommand;
    }


    private RpcServerException createServerException(String errMsg) {
        return new RpcServerException(errMsg);
    }


    private RpcServerException createServerException(String errMsg, Throwable t) {
        String formattedErrMsg = String.format("[Server]OriginErrorMsg: %s: %s. AdditionalErrorMsg: %s", t.getClass().getName(), t.getMessage(), errMsg);
        RpcServerException e = new RpcServerException(formattedErrMsg);
        e.setStackTrace(t.getStackTrace());
        return e;
    }
}